import 'dart:async';
import 'dart:typed_data';

import 'package:flutter/services.dart';

class HprtTscl {
  static const MethodChannel _channel = MethodChannel('hprt_tscl');

  /// 连接打印机
  static Future<int?> portOpen(String portSetting) async {
    final int? res =
        await _channel.invokeMethod('portOpen', {'portSetting': portSetting});
    return res;
  }

  /// 断开连接
  static Future<int?> portClose() async {
    final int? res = await _channel.invokeMethod('portClose');
    return res;
  }

  /// 获取配对的蓝牙设备
  static Future<dynamic> getDevices() async {
    final dynamic res = await _channel.invokeMethod('devices');
    return res;
  }

  /// 蓝牙是否连接
  static Future<int?> isOpened() async {
    final int? res = await _channel.invokeMethod('isOpened');
    return res;
  }

  /// 页标签开始指令
  static Future<int?> printAreaSize(
      {required String width, required String height}) async {
    final int? res = await _channel.invokeMethod('printAreaSize', {
      'width': width,
      'height': height,
    });
    return res;
  }

  /// 页标签结束指令
  static Future<int?> print(
      {required String strnum, required String strcopies}) async {
    final int? res = await _channel.invokeMethod('print', {
      'strnum': strnum,
      'strcopies': strcopies,
    });
    return res;
  }

  /// 打印机自检
  static Future<int?> selfTest() async {
    final int? res = await _channel.invokeMethod('selfTest');
    return res;
  }

  /// 设置标签纸间距
  static Future<int?> gap(
      {required String distance, required String offset}) async {
    final int? res = await _channel.invokeMethod('gap', {
      'distance': distance,
      'offset': offset,
    });
    return res;
  }

  /// 走纸
  static Future<int?> offset(String distance) async {
    final int? res = await _channel.invokeMethod('offset', {
      'distance': distance,
    });
    return res;
  }

  /// 清除打印缓冲区内容
  static Future<int?> cls() async {
    final int? res = await _channel.invokeMethod('cls');
    return res;
  }

  /// 文本打印
  static Future<int?> printText(
      {required String xpos,
      required String ypos,
      String font = '9',
      String rotation = '0',
      String xmultiplication = '0',
      String ymultiplication = '0',
      required String codedata}) async {
    final int? res = await _channel.invokeMethod('printText', {
      'xpos': xpos,
      'ypos': ypos,
      'font': font,
      'rotation': rotation,
      'xmultiplication': xmultiplication,
      'ymultiplication': ymultiplication,
      'codedata': codedata,
    });
    return res;
  }

  /// 文本打印
  static Future<int?> printText1(
      {required String xpos,
      required String ypos,
      String font = '9',
      String rotation = '0',
      String xmultiplication = '0',
      String ymultiplication = '0',
      int alignment = 1,
      required String codedata}) async {
    final int? res = await _channel.invokeMethod('printText1', {
      'xpos': xpos,
      'ypos': ypos,
      'font': font,
      'rotation': rotation,
      'xmultiplication': xmultiplication,
      'ymultiplication': ymultiplication,
      'alignment': alignment,
      'codedata': codedata,
    });
    return res;
  }

  /// 文本打印
  static Future<int?> printText2(
      {required String xpos,
      required String ypos,
      String font = '9',
      String rotation = '0',
      required int size,
      int alignment = 1,
      required String codedata}) async {
    final int? res = await _channel.invokeMethod('printText2', {
      'xpos': xpos,
      'ypos': ypos,
      'font': font,
      'rotation': rotation,
      'size': size,
      'alignment': alignment,
      'codedata': codedata,
    });
    return res;
  }

  /// 打印方向
  static Future<int?> direction(String direction) async {
    final int? res = await _channel.invokeMethod('direction', {
      'direction': direction,
    });
    return res;
  }

  /// 定义标签的参考起点
  static Future<int?> reference(
      {required String xpos, required String ypos}) async {
    final int? res = await _channel.invokeMethod('reference', {
      'xpos': xpos,
      'ypos': ypos,
    });
    return res;
  }

  /// 定义国际字符集代码
  static Future<int?> codepage(String codepage) async {
    final int? res = await _channel.invokeMethod('codepage', {
      'codepage': codepage,
    });
    return res;
  }

  /// 条码
  static Future<int?> printBarcode(
      {required String xpos,
      required String ypos,
      String codetype = '128',
      required String height,
      String readable = '0',
      String rotation = '0',
      String narrow = '1',
      String wide = '1',
      required String codedata}) async {
    final int? res = await _channel.invokeMethod('printBarcode', {
      'xpos': xpos,
      'ypos': ypos,
      'codetype': codetype,
      'height': height,
      'readable': readable,
      'rotation': rotation,
      'narrow': narrow,
      'wide': wide,
      'codedata': codedata,
    });
    return res;
  }

  /// 二维码
  static Future<int?> printQRcode(
      {required String xpos,
      required String ypos,
      String ecclevel = 'Q',
      required String width,
      String mode = 'A',
      String rotation = '0',
      required String codedata}) async {
    final int? res = await _channel.invokeMethod('printQRcode', {
      'xpos': xpos,
      'ypos': ypos,
      'ecclevel': ecclevel,
      'width': width,
      'mode': mode,
      'rotation': rotation,
      'codedata': codedata,
    });
    return res;
  }

  /// 标签进纸指定长度
  static Future<int?> feed(String len) async {
    final int? res = await _channel.invokeMethod('feed', {
      'len': len,
    });
    return res;
  }

  /// 将标签定位到下个标签起始处
  static Future<int?> formfeed() async {
    final int? res = await _channel.invokeMethod('formfeed');
    return res;
  }

  /// 打印线条
  static Future<int?> bar(
      {required String xpos,
      required String ypos,
      required String width,
      required String height}) async {
    final int? res = await _channel.invokeMethod('bar', {
      'xpos': xpos,
      'ypos': ypos,
      'width': width,
      'height': height,
    });
    return res;
  }

  /// 将标签定位到内部传感器确定的起点
  static Future<int?> home() async {
    final int? res = await _channel.invokeMethod('home');
    return res;
  }

  /// 打印图片
  static Future<int?> printImage(
      {required String xpos,
      required String ypos,
      required Uint8List bytes,
      bool isNegate = true,
      bool isLZO = false,
      int type = 0}) async {
    final int? res = await _channel.invokeMethod('printImage', {
      'xpos': xpos,
      'ypos': ypos,
      'bytes': bytes,
      'isNegate': isNegate,
      'isLZO': isLZO,
      'type': type,
    });
    return res;
  }

  /// 打印浓度
  static Future<int?> density(String contrast) async {
    final int? res = await _channel.invokeMethod('density', {
      'contrast': contrast,
    });
    return res;
  }

  /// 打印速度
  static Future<int?> speed(String speed) async {
    final int? res = await _channel.invokeMethod('speed', {
      'speed': speed,
    });
    return res;
  }

  /// 切纸
  static Future<int?> cut() async {
    final int? res = await _channel.invokeMethod('cut');
    return res;
  }

  /// 打印矩形框
  static Future<int?> box(
      {required String xstart,
      required String ystart,
      required String xend,
      required String yend,
      required String thickness}) async {
    final int? res = await _channel.invokeMethod('box', {
      'xstart': xstart,
      'ystart': ystart,
      'xend': xend,
      'yend': yend,
      'thickness': thickness,
    });
    return res;
  }

  /// 设置蜂鸣器鸣叫时间
  static Future<int?> sound(
      {required String level, required String interval}) async {
    final int? res = await _channel.invokeMethod('sound', {
      'level': level,
      'interval': interval,
    });
    return res;
  }

  /// 发送数据
  static Future<int?> writeData(bData) async {
    final int? res = await _channel.invokeMethod('writeData', {'bData': bData});
    return res;
  }

  /// 反白框
  static Future<int?> reverse(
      {required String xstart,
      required String ystart,
      required String xwidth,
      required String yheight}) async {
    final int? res = await _channel.invokeMethod('reverse', {
      'xstart': xstart,
      'ystart': ystart,
      'xwidth': xwidth,
      'yheight': yheight,
    });
    return res;
  }

  /// 文字加粗
  static Future<int?> bold(int bold) async {
    final int? res = await _channel.invokeMethod('bold', {'bold': bold});
    return res;
  }

  /// 获取打印机状态
  static Future<int?> getPrinterStatus() async {
    final int? res = await _channel.invokeMethod('getPrinterStatus');
    return res;
  }

  /// 文本段落打印
  static Future<int?> printBlock(
      {required int startX,
      required int startY,
      required int width,
      required int height,
      required int font,
      int rotation = 0,
      int multiplicationX = 0,
      int multiplicationY = 0,
      int space = 4,
      int alignment = 0,
      required String content}) async {
    final int? res = await _channel.invokeMethod('printBlock', {
      'startX': startX,
      'startY': startY,
      'width': width,
      'height': height,
      'font': font,
      'rotation': rotation,
      'multiplicationX': multiplicationX,
      'multiplicationY': multiplicationY,
      'space': space,
      'alignment': alignment,
      'content': content,
    });
    return res;
  }

  static Future<int?> printQrCodeImage(
      {required String data,
      required int width,
      required int height,
      required String xpos,
      required String ypos,
      bool isNegate = true,
      bool isLZO = false,
      int type = 0}) async {
    final int? res = await _channel.invokeMethod('printQRCodeImage', {
      'data': data,
      'width': width,
      'height': height,
      'xpos': xpos,
      'ypos': ypos,
      'type': type,
      'isNegate': isNegate,
      'isLZO': isLZO,
    });
    return res;
  }
}
